//
//  DHHomepageViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/3.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHHomepageViewController.h"
#import "DHHomepage_HeadCell.h"
#import "DHCommunity_NoImageCell.h"
#import "DHCommunityDetailController.h"

@interface DHHomepageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArr;
@property (assign, nonatomic) NSInteger pageIndex;

@property (strong, nonatomic) NSString *userID;

@end

@implementation DHHomepageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"Ta的主页";
    
    if (self.userDic) {
        self.userID = [self.userDic objectForKey:@"_id"];
    }else{
        self.userID = [Login curLoginUser]._id;
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHHomepage_HeadCell" bundle:nil] forCellReuseIdentifier:kDHHomepage_HeadCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHCommunity_NoImageCell" bundle:nil] forCellReuseIdentifier:kDHCommunity_NoImageCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    [self loadData];
}

- (NSMutableArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSMutableArray array];
    }
    return _itemArr;
}

- (void)loadInfo{
    [[DHNetAPIClient shareDHNetAPIClient] request_social_user_infoWithParams:@{@"user_id":self.userID} andBlock:^(id data, NSError *error) {
        self.userDic = [data objectForKey:@"info"];
        [self.tableView reloadData];
    }];
}

- (void)loadData{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    [self.itemArr removeAllObjects];
    [[DHNetAPIClient shareDHNetAPIClient] request_social_postsWithParams:@{@"user_id":self.userID,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        if (data) {
            [self.itemArr addObjectsFromArray:[data objectForKey:@"posts"]];
            if (self.itemArr.count < 20) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                self.pageIndex += 1;
            }
            [self.tableView reloadData];
        }
    }];
}

- (void)loadMoreData{
    [[DHNetAPIClient shareDHNetAPIClient] request_social_postsWithParams:@{@"user_id":[self.userDic objectForKey:@"_id"],@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        if (data) {
            [self.itemArr addObjectsFromArray:[data objectForKey:@"posts"]];
            if (self.itemArr.count < (self.pageIndex + 1) * 20) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                self.pageIndex += 1;
            }
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArr.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 200;
    }else{
        NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row - 1];
        NSArray *images = [model objectForKey:@"images"];
        
        //计算文字高度
        CGFloat contentHeight = [[model objectForKey:@"content"] heightForFont:[UIFont systemFontOfSize:14] width:kScreenWidth - 20];
        //如果内容中有 \n 等转义字符
        NSInteger specialChar = 0;//出现次数
        specialChar = [[model objectForKey:@"content"] countOccurencesOfString:@"\n"];
        CGFloat textHeight = contentHeight + specialChar * 14;
        
        //图片CollectionView高度
        CGFloat imgsHeight = 0;
        if (images.count == 0) {
            imgsHeight = 0;
        }else if (images.count > 0 && images.count <= 3){
            imgsHeight = 128;
        }else if (images.count > 3 && images.count <= 6){
            imgsHeight = 128 * 2 + 5;
        }else if (images.count > 6 && images.count <= 9){
            imgsHeight = 128 * 3 + 10;
        }
        
        return imgsHeight + textHeight + 65;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHHomepage_HeadCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHHomepage_HeadCell forIndexPath:indexPath];
        [cell updateCell:self.userDic];
        return cell;
    }else{
        DHCommunity_NoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHCommunity_NoImageCell forIndexPath:indexPath];
        [cell updateCell:[self.itemArr objectAtIndex:indexPath.row - 1]];
        cell.row = indexPath.row;
        cell.headViewLayoutHeight.constant = 0;
        cell.headView.hidden = YES;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row != 0){
        NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row - 1];
        DHCommunityDetailController *vc = [[DHCommunityDetailController alloc] initWithCommunityDic:model];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
